<script src="__PUBLIC__/javascripts/admin/layer/layer.js"></script>
<div class="Item hr">
    <div class="current">模型基本信息</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table1">
    <tr>
        <th width="100">模型名称：</th>
        <td>{$model['name']}</td>
    </tr>
    <tr>
        <th width="100">数据表名：</th>
        <td>{$model['tbl_name']}</td>
    </tr>
    <tr>
        <th width="100">是否为内部表：</th>
        <td>
            <if condition="$model['is_inner'] == 1">是
                <else/>
                否
            </if>
        </td>
    </tr>
    <tr>
        <th width="100">字段数：</th>
        <td>{$model['fields_count']}</td>
    </tr>
    <tr>
        <th width="100">记录行数：</th>
        <td>{$model['rows']}</td>
    </tr>
    <tr>
        <th>模型描述：</th>
        <td>{$model['description']}</td>
    </tr>
</table>

<div class="Item hr">
    <div class="current">模型字段信息</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab">
    <thead>
    <tr>
        <td>表单域顺序</td>
        <td>字段名称</td>
        <td>字段标签</td>
        <td>字段类型</td>
        <td>是否必填</td>
        <td>是否唯一</td>
        <td>是否索引</td>
        <td>系统字段</td>
        <td>字段操作</td>
    </tr>
    </thead>
    <tbody>
    <volist name="model['fields']" id="field">
        <tr align="center">
            <td>
                <if condition="0 == $field['show_order']">---
                    <else/>
                    {$field['show_order']}
                </if>
            </td>
            <td>{$field['name']}</td>
            <td>{$field['comment']}</td>
            <td>{$field['type']}</td>
            <td>
                <if condition="$field['is_require'] eq 1">是
                    <else/>
                    否
                </if>
            </td>
            <td>
                <if condition="$field['is_unique'] eq 1">是
                    <else/>
                    否
                </if>
            </td>
            <td>
                <if condition="$field['is_index'] eq 1">是
                    <else/>
                    否
                </if>
            </td>
            <td>
                <if condition="$field['is_system'] eq 1">是
                    <else/>
                    否
                </if>
            </td>
            <td>
                <if condition="$field['is_system'] eq 0">
                    [ <a href="{:U('Fields/edit', array('model_id' => $model['id'], 'field_id' => $field['id']))}">编辑</a>
                    ] [ <a href="{:U('Fields/delete', array('model_id' => $model['id'], 'field_id' => $field['id']))}" name="{$field['name']}" class="del">删除</a> ]
                <else/>
                    <a href="javascript:void(0);" class="toggle"
                       value="{:U('Fields/toggleListShow', array('field_id' => $field['id']))}"
                       status="{$field['is_list_show']}" name="{$field['name']}">
                        <if condition="$field['is_list_show'] eq 1">
                            不在数据列表中显示
                            <else/>
                            数据列表中显示
                        </if>
                    </a>
                </if>
            </td>
        </tr>
    </volist>
    </tbody>
</table>
<div class="commonBtnArea">
    <button class="btn submit" onclick="window.location.href='{:U('Fields/add', array('model_id' => $model['id']))}'">
        添加字段
    </button>

    <button class="btn table_sync">
        检查表差异
    </button>
</div>

<script type="text/javascript">
    $(function () {
        id = {$model['id']};
        name = "{$model['tbl_name']}";

        $(document).on("click", ".do_sync", function () {
            $.post("{:U('Models/sync_model')}", {"id": id, "name": name}, function (d) {
                if(d.status==1){
                    $(".layui-layer-close").click();
                    popup.success(d.info);
                    setTimeout(function(){
                        popup.close("asyncbox_success");
                    },2000);
                }else{
                    popup.error(d.info);
                    setTimeout(function(){
                        popup.close("asyncbox_error");
                    },2000);
                }

                if(d.url && d.url!=''){
                    setTimeout(function(){
                        window.location.href=d.url;
                    },2000);
                }
            });
        });

        $(".table_sync").click(function () {
            // 查询表差异
            $.post("{:U('Models/check_table')}", {"id": id, "name": name}, function (d) {

                layer.open({
                    type: 1,
                    area: ['600px', '360px'],
                    shadeClose: true, //点击遮罩关闭,
                    scrollbar: false,
                    content: '\<\div style="padding:20px;">'+ d.info.html +'\<\/div>',
                });
            });
        });

        $(".del").click(function () {
            var url = $(this).attr("href");
            popup.confirm('你真的打算删除 <b>' + $(this).attr("name") + '</b> 吗?', '温馨提示', function (action) {
                if (action == 'ok') {
                    $.getJSON(url, function (json) {
                        if (json.status) {
                            popup.success(json.info);
                            setTimeout(function () {
                                popup.close("asyncbox_success");
                                top.window.location.reload();
                            }, 2000);
                        } else {
                            popup.error(json.info);
                            setTimeout(function () {
                                popup.close("asyncbox_error");
                            }, 2000);
                        }
                    });
                }
            });
            return false;
        });

        $(".toggle").click(function () {
            var url = $(this).attr("value");
            var name = $(this).attr("name");
            var status = $(this).attr("status") || 0;
            status = status == 0 ? '显示' : '不显示';

            popup.confirm('您确定在数据列表中' + status + " <b>" + name + "</b> 字段吗？", '温馨提示', function (action) {
                if (action == 'ok') {
                    $.getJSON(url, function (json) {
                        if (json.status) {
                            popup.success(json.info);
                            setTimeout(function () {
                                popup.close("asyncbox_success");
                            }, 2000);
                        } else {
                            popup.error(json.info);
                            setTimeout(function () {
                                popup.close("asyncbox_error");
                            }, 2000);
                        }

                        setTimeout(function () {
                            top.window.location.reload();
                        }, 1000);
                    });
                }
            });

            return false;
        });
    });
</script>

<style>
    .field_diff_box {
        padding:5px;
        margin-bottom:25px;
    }
    .field_diff_box h3 {
        font-weight:normal;
        margin-bottom:10px;
    }
    .field_diff_box ul{
        margin-bottom:10px;
    }
    .field_diff_box strong{
        font-weight:normal;
        display: block;
        margin-bottom: 5px;
        padding-bottom:5px;
        border-bottom:1px dashed #ddd;
    }
    .field_diff_box li {
        padding: 5px 0;
    }
    .field_diff_box .do_sync {
        position: fixed;
        bottom:15px;
        right:15px;
    }
</style>
